package com.qf.xiaomi.web.servlet.admin;

import com.qf.xiaomi.pojo.TbGoodsType;
import com.qf.xiaomi.service.goodsTypeService;
import com.qf.xiaomi.service.impl.goodsTypeServiceImpl;
import com.qf.xiaomi.utils.StringConstans;
import com.qf.xiaomi.web.base.AdminBaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 田贵宾 on 2022/8/21 17:02
 *
 * @author 田贵宾
 * @version 1.0
 * //TODO java练习阶段
 * @since 1.0
 */
@WebServlet("/getGoodsType")
public class GoodsTypeServlet extends AdminBaseServlet{
    goodsTypeService service= new goodsTypeServiceImpl();
    public String show(HttpServletRequest req, HttpServletResponse resp){
        List<TbGoodsType> goodsTypes= new ArrayList<>();
        goodsTypes = service.findType(goodsTypes);
        req.setAttribute("goodsTypes",goodsTypes);
        return StringConstans.FORWARD+"/admin/showGoodsType.jsp";
    }
    public String add(HttpServletRequest req,HttpServletResponse resp){
        String parentStr = req.getParameter("Parent");

        long parent = Long.parseLong(parentStr);
        String name = req.getParameter("name");

        TbGoodsType tbGoodsType = new TbGoodsType();
        tbGoodsType.setParent(parent);
        tbGoodsType.setName(name);
        tbGoodsType = service.addType(tbGoodsType);


        List<TbGoodsType> goodsTypes  = (List<TbGoodsType>) req.getServletContext().getAttribute("goodsTypeList");
        goodsTypes.add(tbGoodsType);
        return StringConstans.REDIRECT+"/admin/showGoodsType.jsp";
    }
    public String delete(HttpServletRequest req,HttpServletResponse resp){
        String id = req.getParameter("id");
        long TypeId = Long.parseLong(id);
        List<TbGoodsType>  tbGoodsType = new ArrayList<>();
        tbGoodsType = service.delByTypeId(tbGoodsType, TypeId);
        req.setAttribute("goodsTypeList",tbGoodsType);
        return StringConstans.FORWARD+"/admin/showGoodsType.jsp";
    }
}
